/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.wicket.IClusterable;

/**
 *
 * @author Jan Korreck
 */
public class MyItemPackage implements Serializable, IClusterable {

    private String id;
    private String title;
    private HashMap<String, Integer> items = new HashMap<String, Integer>(); // ID, count
    private double packagePrice;
    private int discount;
    private boolean reserved = false;

    public MyItemPackage() {
    }

    public MyItemPackage(String id, String title) {
        this.id = id;
        this.title = title;
    }

    public void addItem(String itemId, int count) {

        if (this.items.containsKey(itemId)) {
            // Hier ueberpruefen, ob item verfuegbar
            int oldCount = this.items.get(itemId).intValue();
            int newCount = oldCount + count;
            this.items.put(itemId, newCount);

        } else {

            // Hier ueberpruefen, ob item verfuegbar
            this.items.put(itemId, count);
        }
        this.calculatePrice();
    }

    public void increaseItem(String itemId) {
        if (this.items.containsKey(itemId)) {
            int oldCount = this.items.get(itemId).intValue();
            System.out.println("OLDCOUNT: " + oldCount);
            int newCount = oldCount + 1;

            this.items.put(itemId, newCount);
            System.out.println("NEWCOUNT: " + this.items.get(itemId).intValue());
        }
        // Itemcount wieder erhoehen => ueber itemManager
        this.calculatePrice();
    }

    public void decreaseItem(String itemId) {

        if (this.items.containsKey(itemId)) {
            int oldCount = this.items.get(itemId).intValue();

            int newCount = (oldCount - 1) < 0 ? 0 : (oldCount - 1);


                this.items.put(itemId, newCount);
        }
        // Itemcount wieder erhoehen => ueber itemManager
        this.calculatePrice();
    }

    public void removeItem(String itemId, int count) {

        if (this.items.containsKey(itemId)) {
            int oldCount = this.items.get(itemId).intValue();

            int newCount = (oldCount - count);


            if (newCount <= 0) {
                this.items.remove(itemId);
            } else {

                this.items.put(itemId, newCount);
            }
        }
        // Itemcount wieder erhoehen => ueber itemManager
        this.calculatePrice();
    }

    public void removeItem(String itemID) {
        this.items.remove(itemID);
        this.calculatePrice();
    }

    public int getDiscount() {
        return discount;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDiscount(int discount) {

        // discount preufen und dann neuen preis berechnen
        if (discount >= 0 && discount <= 80) {
            this.discount = discount;
            this.calculatePrice();
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getPackagePrice() {
        return packagePrice;
    }

    public void setPackagePrice(double price) {
        this.packagePrice = price;
    }

    public boolean getReserved() {
        return reserved;
    }

    public void setReserved(boolean reserved) {
        this.reserved = reserved;
    }

    public HashMap<String, Integer> getItems() {
        return items;
    }

    public List<MyItem> getItemList() {
        ArrayList<MyItem> content = new ArrayList<MyItem>();

        Iterator<String> keyIt = this.items.keySet().iterator();

        while (keyIt.hasNext()) {
            content.add(MyItemManager.getInstance().getItem(keyIt.next()));
        }

        return content;
    }

    private void calculatePrice() {
        Iterator<String> ids = this.items.keySet().iterator();
        double completePrice = 0;
        String itemId = "";

        while (ids.hasNext()) {
            itemId = ids.next();
            completePrice += this.items.get(itemId) * MyItemManager.getInstance().getItem(itemId).getPrice();
        }
        completePrice -= completePrice * (discount / 100.0d);
        this.packagePrice = completePrice;
    }

    public void updatePackage() {

        Iterator<String> itemIt = this.items.keySet().iterator();
        String actItem;
        while(itemIt.hasNext()) {
            actItem = itemIt.next();

            if (this.items.get(actItem).intValue() == 0) {
                this.items.remove(actItem);
            }
        }
    }

    public boolean containsItem(String itemId) {
        return this.items.containsKey(itemId);
    }
    
}
